package com.newbsd.web.maventest.mvc;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.omg.CORBA.Request;
/**
 * 
 * @author 张成玉-内部网页跳转
 *
 */
public class InnerDispatcherController extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4813120068565234960L;
	private Map<String, Method> actions=new HashMap<>();
	/**
	 * (non-Javadoc)
	 * @see javax.servlet.GenericServlet#init()
	 */
	@Override
	public void init() throws ServletException {
		// TODO Auto-generated method stub
		super.init();
		Method[] methods = this.getClass().getMethods();
		Method method = null;
		for(int i= 0;i<methods.length;i++){
			method = methods[i];
			if(method.getModifiers() == Modifier.PUBLIC){
				actions.put(this.getServletContext().getContextPath()+"/"+this.getServletName()+
						"/"+method.getName(), method);
			}
		}
		
	}
	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * javax.servlet.http.HttpServlet#service(javax.servlet.http.HttpServletRequest,
	 * javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String uri = request.getRequestURI();
		Method action = actions.get(uri);
		if(null != action){
			try{
				action.invoke(this,request,response);
			}catch(IllegalAccessException|IllegalArgumentException|InvocationTargetException e){
				e.printStackTrace();
			}
		}
	}
	

}
